package q;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class ch6_6 extends JFrame implements KeyListener {
    JPanel panel;
    Color c;
    JLabel label;
    JTextField textField;
    public ch6_6() {
        panel = new JPanel();
        label = new JLabel("输入r、g、b、y,可改变颜色");
        textField = new JTextField(10);
        panel.add(label);
        panel.add(textField);
        this.add(panel);
        this.setVisible(true);
        this.setSize(400, 300);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        textField.addKeyListener(this);
    }
    public static void main(String[] args) {
        new ch6_6();
    }
    @Override
    public void keyTyped(KeyEvent e){
    }
    @Override
    public void keyPressed(KeyEvent e){
        if(e.getKeyChar()=='r')
            c= Color.RED;
            else if(e.getKeyChar()=='g')
                c= Color.GREEN;
            else if(e.getKeyChar()=='b')
                c= Color.BLUE;
            else if(e.getKeyChar()=='y')
                c= Color.YELLOW;
            panel.setBackground(c);
    }
    @Override
    public void keyReleased(KeyEvent e){}
}
